package vn.com.servicedirectory.service.impl;

import javax.inject.Inject;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import vn.com.servicedirectory.common.CommonFunction;
import vn.com.servicedirectory.model.GovernmentOffice;
import vn.com.servicedirectory.repository.GovRegionOfficceRepository;
import vn.com.servicedirectory.service.GovOfficeRegionService;

@Service
public class GovOfficeRegionServiceImpl implements GovOfficeRegionService {
	@Inject
	protected GovRegionOfficceRepository govOfficeRe;

	@Override
	@Transactional(readOnly = true)
	public Page<GovernmentOffice> findByName(String name, int page, int size,
			boolean active) {
		Pageable pageable = new PageRequest(page, size, new Sort(
				Direction.DESC, "govOfficeRegionName"));
		Page<GovernmentOffice> govOfficePage;
		if ("all".equals(name)) {
			// find all
			govOfficePage = govOfficeRe.findByActive(active, pageable);
		} else {
			// find each
			// govOfficePage=govOfficeRe.findByActive(active, pageable);
			govOfficePage = govOfficeRe.findByName(CommonFunction.checkString(name.charAt(0)),CommonFunction.checkString(name.charAt(1)), true, pageable);
		}
		return govOfficePage;
	}

	@Override
	public GovernmentOffice findById(int id) {
		GovernmentOffice govOffice = govOfficeRe.findOne(id);
		return govOffice;
	}

}
